package com.fzy.common.redis.impl;

import com.alibaba.fastjson.JSON;
import com.fzy.common.bean.ObjectUtil;
import com.fzy.common.redis.JedisClient;
import com.fzy.common.util.FastJsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;


/**
 * @author lishw
 * @create 2017-01-18
 */

public class JedisClientSingle implements JedisClient {

    @Autowired
    private JedisPool jedisPool;

    @Value("${spring.redis.password}")
    private String password;

    private Jedis getResource() {
        Jedis jedis = jedisPool.getResource();
        if (StringUtils.isBlank(password)) {
            return jedis;
        } else {
            jedis.auth(password);
            return jedis;
        }
    }

    @Override
    public String get(String key) {

        Jedis jedis = getResource();

        String string = jedis.get(key);

        jedis.close();

        return string;

    }

    @Override
    public String set(String key, String value) {

        Jedis jedis = getResource();

        String string = jedis.set(key, value);

        jedis.close();

        return string;

    }

    @Override
    public String hget(String hkey, String key) {

        Jedis jedis = getResource();

        String string = jedis.hget(hkey, key);

        jedis.close();

        return string;

    }

    @Override
    public long hset(String hkey, String key, String value) {

        Jedis jedis = getResource();

        long hset = jedis.hset(hkey, key, value);

        jedis.close();

        return hset;

    }

    @Override
    public long incr(String key) {

        Jedis jedis = getResource();

        long incr = jedis.incr(key);

        jedis.close();

        return incr;

    }

    @Override
    public long expire(String key, Integer second) {

        Jedis jedis = getResource();

        long expire = jedis.expire(key, second);

        jedis.close();

        return expire;

    }

    @Override
    public long ttl(String key) {

        Jedis jedis = getResource();

        long ttl = jedis.ttl(key);

        jedis.close();

        return ttl;
    }

    @Override
    public long del(String key) {

        Jedis jedis = getResource();

        long del = jedis.del(key);

        jedis.close();

        return del;
    }

    @Override
    public long hdel(String hkey, String key) {

        Jedis jedis = getResource();

        long hdel = jedis.hdel(hkey, key);

        jedis.close();

        return hdel;
    }

    @Override
    public String setObject(String key, Object object) {
        Jedis jedis = getResource();

        String str = jedis.set(key.getBytes(), ObjectUtil.serialize(object));

        jedis.close();

        return str;
    }

    @Override
    public <T> T getObject(String key) {

        Jedis jedis = getResource();

        byte[] bytes = jedis.get(key.getBytes());

        jedis.close();

        if (null != bytes) {
            return ObjectUtil.unserialize(bytes);
        }

        return null;
    }

    @Override
    public String setJson(String key, Object object) {
        Jedis jedis = getResource();

        String str = jedis.set(key, FastJsonUtil.toJson(object));

        jedis.close();

        return str;
    }

    @Override
    public <T> T getJson(String key, Class<T> clazz) {
        Jedis jedis = getResource();

        String data = jedis.get(key);

        jedis.close();

        return JSON.parseObject(data, clazz);
    }

    /**
     * 同时将多个field - value(域-值)对设置到哈希表key中
     *
     * @param key
     * @param hash
     * @return
     * @author 冯赵杨
     * @create 2018/9/7
     */
    @Override
    public String hmset(String key, Map<String, String> hash) {
        Jedis jedis = getResource();

        String data = jedis.hmset(key, hash);

        jedis.close();
        return data;
    }

    /**
     * 返回哈希表key中，一个或多个给定域的值
     *
     * @param key
     * @param fields
     * @return
     * @author 冯赵杨
     * @create 2018/9/7
     */
    @Override
    public List<String> hmget(String key, String... fields) {
        Jedis jedis = getResource();

        List<String> data = jedis.hmget(key, fields);

        jedis.close();

        return data;
    }

    /**
     * 返回哈希表key中，所有的域和值
     *
     * @param key
     * @return
     * @author 冯赵杨
     * @create 2018/9/7
     */
    @Override
    public Map<String, String> hgetAll(String key) {
        Jedis jedis = getResource();

        Map<String, String> data = jedis.hgetAll(key);

        jedis.close();

        return data;
    }
}
